package org.dromara.third.convert.riskUnit.web;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.RiskUnit;
import org.dromara.system.mapper.RiskUnitMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.riskUnit.dto.RiskUnitDTO;
import org.dromara.third.convert.riskUnit.validator.RiskUnitValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertRiskUnitController {

    @Autowired
    private RiskUnitMapper riskUnitMapper;

    @PostMapping("/riskUnit")
    @Transactional
    @InterceptorIgnore(tenantLine = "true", dataPermission = "true")
    public R<Void> addRiskUnit(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        RiskUnitDTO riskUnitDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            riskUnitDTO = JSONObject.parseObject(decrypt, RiskUnitDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, riskUnitDTO, new RiskUnitValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        RiskUnit riskUnit = assembleRiskUnitData(orgCode, riskUnitDTO);
        int count = riskUnitMapper.countWithOutDelFlag(riskUnit.getId());
        if (count == 0) {
            riskUnitMapper.insert(riskUnit);
        } else {
            riskUnitMapper.updateWithOutDelFlag(riskUnit);
        }
        return R.ok();
    }

    private RiskUnit assembleRiskUnitData(String orgCode, RiskUnitDTO dto) {
        RiskUnit riskUnit = new RiskUnit();
        try {
            riskUnit.setId(dto.getId());
            riskUnit.setUnitName(dto.getUnitName());
            riskUnit.setRiskAnalysisId(dto.getRiskAnalysisId());
            riskUnit.setHazardDept(dto.getHazardDept());
            riskUnit.setHazardLiablePerson(dto.getHazardLiablePerson());
            riskUnit.setDelFlag(Long.parseLong(dto.getDelFlag()));
            riskUnit.setCreateDept(Long.parseLong(dto.getCreateDept()));
            riskUnit.setCreateBy(Long.parseLong(dto.getCreateBy()));
            riskUnit.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            riskUnit.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            riskUnit.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            riskUnit.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装风险分析单元信息异常");
        }
        return riskUnit;
    }
}
